﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ControlLibrary
{
    public class NavigationControl : ItemsControl
    {
        public NavigationControl()
        {
            this.DefaultStyleKey = typeof(NavigationControl);
        }

        public static readonly DependencyProperty SelectedIndexProperty = DependencyProperty.Register("SelectedIndex", typeof(int), typeof(NavigationControl), new PropertyMetadata(0));
		public int SelectedIndex
		{
    		get { return (int) base.GetValue(SelectedIndexProperty); }
    		set { base.SetValue(SelectedIndexProperty, value); }
		}

        public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(object), typeof(NavigationControl), new PropertyMetadata(null));
		public object SelectedItem
		{
    		get { return base.GetValue(SelectedItemProperty); }
    		set { base.SetValue(SelectedItemProperty, value); }
		}

        public static readonly DependencyProperty TransitionStyleProperty = DependencyProperty.Register("TransitionStyle", typeof(Style), typeof(NavigationControl), new PropertyMetadata(null));
		public Style TransitionStyle
		{
			get { return (Style)GetValue(TransitionStyleProperty); }
			set { SetValue(TransitionStyleProperty, value); }
		}

        public static readonly DependencyProperty ReversedProperty = DependencyProperty.Register("Reversed", typeof(bool), typeof(NavigationControl), new PropertyMetadata(false));
		public bool Reversed
		{
			get { return (bool)GetValue(ReversedProperty); }
			set { SetValue(ReversedProperty, value); }
		}

        public static readonly DependencyProperty InCarouselModeProperty = DependencyProperty.Register("InCarouselMode", typeof(bool), typeof(NavigationControl), new PropertyMetadata(true));
		public bool InCarouselMode
		{
			get { return (bool)GetValue(InCarouselModeProperty); }
			set { SetValue(InCarouselModeProperty, value); }
		}
		
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();

            SelectedItem = this.Items[SelectedIndex];
			
			Button prevButton = this.GetTemplateChild("NextButton") as Button;
			if (prevButton != null)
			{
				prevButton.Click += new System.Windows.RoutedEventHandler(prevButton_Click);
			}

			Button nextButton = this.GetTemplateChild("PreviousButton") as Button;
			if (nextButton != null)
			{
				nextButton.Click += new System.Windows.RoutedEventHandler(nextButton_Click);
			}
		}

        //protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        //{
        //    base.OnItemsChanged(e);

        //    if (this.Items.Count > 0 & _selectionBox != null)
        //    {
        //        _selectionBox.ItemsSource = this.Items;
        //        _selectionBox.SelectedItem = this.Items[this.SelectedIndex];
        //        selectionBox_SelectionChanged(null, null);
        //    }
        //}

		private void prevButton_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			this.Reversed = this.InCarouselMode;
			SelectedIndex = (this.SelectedIndex - 1 + this.Items.Count) % (this.Items.Count);
			SelectedItem = this.Items[this.SelectedIndex];
		}

		private void nextButton_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			this.Reversed = false;
			SelectedIndex = (this.SelectedIndex + 1) % (this.Items.Count);
			SelectedItem = this.Items[this.SelectedIndex];
		}
    }
}
